package com.ptp.backendcertms.networksecurity.filter.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;

import static com.ptp.backendcertms.api.internal.constant.CertMsConstant.*;

/**
 * Request工具，获取请求的body
 *
 * @author songcongcong
 * @date 2022/3/9
 */
@Slf4j
public class CertMsRequestUtil {
    private static final String UNKNOWN = "unknown";


    /***
     * 获取 Http请求 body信息
     * @param request http请求
     * @return body信息
     */
    public static String getBody(HttpServletRequest request) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            int len = request.getContentLength();
            if (len != -1) {
                br = request.getReader();
                String str;
                while ((str = br.readLine()) != null) {
                    sb.append(str);
                }
                br.close();
            }
        } catch (IOException e) {
            if (e instanceof ClientAbortException || e instanceof EOFException) {
                // EOFException表示流到末尾 修改打印日志级别
                // ClientAbortException表示客户端关闭无法写入数据
                log.warn("[CertMsRequestUtil-EOFException]-getBody", e);
            } else {
                log.error("[CertMsRequestUtil]-getBody", e);
            }
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    log.error("[CertMsRequestUtil]-getBody-close", e);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 获取请求中的的ip
     *
     * @param request 请求参
     * @return ip
     */
    public static String getIpAddress(HttpServletRequest request) {
        if (null == request) {
            return "";
        }
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }

        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if (ip != null && ip.contains(ENGLISH_COMMA_SEPARATOR)) {
            ip = ip.split(ENGLISH_COMMA_SEPARATOR)[0];
        }
        return ip;
    }


    /**
     * Title: crossDomainRequest
     * Description: 放过跨域请求
     */
    public static void crossDomainRequest(HttpServletResponse resp) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        resp.setHeader("Access-Control-Max-Age", "3600");
        resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization");
        resp.setHeader("Access-Control-Request-Headers", "POST");
    }
}
